package com.guessdroid.util;


import java.text.SimpleDateFormat;
import java.util.Date;



public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
	
	public void uncaughtException(Thread thread, Throwable exception) {
		writeException(exception);
	}
	
	private static String getStackTrace(Throwable exception){
		StringBuilder builder = new StringBuilder();
	    for (StackTraceElement element : exception.getStackTrace()) {
	        builder.append(element.toString());
	        builder.append("\n");
	    }
		return builder.toString();
	}
	
	public static void writeException(Throwable exception){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd_MM_HHmm");
		Date date = new Date();
		String message= "Message: "+ exception.getMessage()+"\n";
		if(exception.getCause()!=null)
			message += "Cause: "+ exception.getCause().toString() +getStackTrace(exception.getCause());
		message += "StackTrace: " + getStackTrace(exception);
		AndroidWriter.writeTextToFile(message, "error_"+ dateFormat.format(date)+".txt");
	}
   
}